mui.plusReady(function() {
	var urlPrefix = "https://bird.ioliu.cn/v1?url=",
		currentDepartmentId = "11",
		storage = plus.storage;
	pageRefresh(currentDepartmentId);

	// 请求侧滑科室列表数据
	mui.ajax(urlPrefix + "http://app.xiaoweicanting.com/hospital.php?m=App&c=index&a=getDepartmentList&hospitalid=10&uid=3&token=dfaf", {
		async: true,
		type: "GET",
		dataType: "json",
		beforeSend: function() {
			if(plus.networkinfo.getCurrentType() === 1) {
				var departmentList = storage.getItem("department_list_in_equipment_manage");
				if(departmentList) {
					var data = JSON.parse(departmentList);
					renderOffsetMenu(data);
				}
				return false;
			}
		},
		success: function(data, textStatus) {
			if(textStatus === "success") {
				var data = data.data;
				if(data.length) {
					renderOffsetMenu(data);
					storage.setItem("department_list_in_equipment_manage", JSON.stringify(data));
				}
			}
		}
	});
	
	function renderOffsetMenu(data) {
		var html = "<ul class='mui-table-view'>",
			list = document.getElementById("offset-menu-list");
		for(var i = 0, length = data.length; i < length; i++) {
			html += "<li class='mui-table-view-cell' data-departmentid='" + data[i].id + "'>" + data[i].name + "</li>";
		}
		html += "</ul>";
		list.innerHTML += html;
		
		var lis = list.querySelectorAll("li");
		[].slice.call(lis).forEach(function(v) {
			v.addEventListener("tap", function() {
				currentDepartmentId = v.getAttribute("data-departmentid");
				mui('.mui-off-canvas-wrap').offCanvas().close();
				pageRefresh(currentDepartmentId);
				var addActive = removeActive = false;
				for(var i = 0, length = lis.length; i < length; i++) {
					if(!removeActive && lis[i].classList.contains("active-item")) {
						if(lis[i].getAttribute("data-departmentid") === currentDepartmentId) break;
						lis[i].classList.remove("active-item");
						removeActive = true;
						if(addActive) break;
					}
					if(!addActive && lis[i].getAttribute("data-departmentid") === currentDepartmentId) {
						lis[i].classList.add("active-item");
						addActive = true;
						if(removeActive) break;
					}
				}
			});
		});
	}
});

function pageRefresh(departmentid) {
	var urlPrefix = "https://bird.ioliu.cn/v1?url=",
		storage = plus.storage;
	mui.ajax(urlPrefix + "http://app.xiaoweicanting.com/hospital.php?m=App&c=index&a=getEquipmentStatInfo&hospitalid=10&departmentid=" + departmentid + "&uid=3&token=dfaf", {
		async: true,
		type: "GET",
		dataType: "json",
		beforeSend: function() {
			if(plus.networkinfo.getCurrentType() === 1) {
				var lineChartsData = storage.getItem("lineChartsData_homepage");
				if (lineChartsData) {
					var data = JSON.parse(lineChartsData);
					drawLineChart(data);
				} else {
					mui.toast("网络好像有点小问题，\r\n请检查网络连接~");
				}
				return false;
			}
		},
		success: function(data, textStatus) {
			if(textStatus === "success") {
				if (data) {
					drawLineChart(data);
					storage.setItem("lineChartsData_homepage", JSON.stringify(data));	
				}
			}
		}
	});
	
	// 请求工单数量变化趋势
	mui.ajax(urlPrefix + "http://app.xiaoweicanting.com/hospital.php?m=App&c=index&a=getFaultOrderStatInfo&hospitalid=10&departmentid=" + departmentid + "&uid=3&token=dfaf", {
		async: true,
		type: "GET",
		dataType: "json",
		beforeSend: function() {
			if(plus.networkinfo.getCurrentType() === 1) {
				var barChartsData = storage.getItem("barChartsData_homePage");
				if (barChartsData) {
					var data = JSON.parse(barChartsData);
					drawBarChart(data);
				} else {
					mui.toast("网络好像有点小问题，\r\n请检查网络连接~");
				}
				return false;
			}
		},
		success: function(data, textStatus) {
			if(textStatus === "success") {
				if (data) {
					drawBarChart(data);
					storage.setItem("barChartsData_homePage", JSON.stringify(data));	
				}
			}
		}
	});
	
	// 请求饼图数据
	mui.ajax(urlPrefix + "http://app.xiaoweicanting.com/hospital.php?m=App&c=statistic&a=getEquipmentStateStatInfo&hospitalid=10&departmentid=" + departmentid + "&uid=3&token=dfaf", {
		async: true,
		type: "GET",
		dataType: "json",
		beforeSend: function() {
			if(plus.networkinfo.getCurrentType() === 1) {
				var pieChartsData = storage.getItem("pieChartsData_statistics");
				if (pieChartsData) {
					var data = JSON.parse(pieChartsData);
					drawPieChart(data);
				} else {
					mui.toast("网络好像有点小问题，\r\n请检查网络连接~");
				}
				return false;
			}
		},
		success: function(data, textStatus) {
			
			if(textStatus === "success") {
				var data = data.data;
				if (data.length) {
					drawPieChart(data);
					storage.setItem("pieChartsData_statistics", JSON.stringify(data));	
				}
			}
		}
	});
}

function drawLineChart(data) {	
	var ctx = document.getElementById("myMingxiChart");
	var myMingxiChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: data.xdata,
    datasets: [
        {
            label: "工单数量",
            fill: true,
            lineTension: 0.4,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
            data: data.ydata,
            spanGaps: false,
        }
    ]
}
});


}

function drawBarChart(data) {
	
	var ctx = document.getElementById("myEquipStat");
	var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: data.xdata,
        datasets: [{
            label: '设备统计',
            data: data.ydata,
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

}



    
function drawPieChart(data) {

	var randomScalingFactor = function() {
      return Math.round(Math.random() * 100);
  };
  
	var config = {
      type: 'pie',
      data: {
          datasets: [{
              data: [
                  randomScalingFactor(),
                  randomScalingFactor(),
                  randomScalingFactor(),
                  randomScalingFactor(),
                  randomScalingFactor(),
              ],
              backgroundColor: [
                 'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)'
              ],
              label: 'Dataset 1'
          }],
          labels: [
              "Red",
              "Orange",
              "Yellow",
              "Green",
              "Blue"
          ]
      },
      options: {
          responsive: true
      }
  };


	var ctx = document.getElementById("myEquipPieStat");
//	// For a pie chart
	var myPieChart = new Chart(ctx,config);
	
	
	var xdata = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
	var ydata = [126, 123, 521, 341, 234, 442, 190];
		
	var ctx = document.getElementById("myEquipCuvStat");
	var myMingxiChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: xdata,
    datasets: [
        {
            label: "工单数量",
            fill: true,
            lineTension: 0.4,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
            data: ydata,
            spanGaps: false,
        }
    ]
}
});


}
